//
//  HelloWorldLayer.m
//  HelloWorldGame
//
//  Created by huji on 13-1-2.
//  Copyright __MyCompanyName__ 2013年. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"
#import "SimpleAudioEngine.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "GameMovingObject.h"
#import "GameLayer.h"

#pragma mark - HelloWorldLayer

@interface HelloWorldLayer ()
@property (nonatomic,assign) GameLayer *glayer;
@end

// HelloWorldLayer implementation
@implementation HelloWorldLayer{
    CCTMXTiledMap *_tileMap;
    CCTMXLayer *_backGroundLayer;
    GameMovingObject *_player;
}

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
    GameLayer *glayer = [GameLayer node];
    layer.glayer=glayer;
	
	// add layer as a child to scene
	[scene addChild:layer];
    [scene addChild:glayer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
	if( (self=[super init]) ) {
		
		_tileMap = [[CCTMXTiledMap tiledMapWithTMXFile:@"Map.tmx"] retain];
        _backGroundLayer = [[_tileMap layerNamed:@"Background"] retain];
        [self addChild:_tileMap];
        
        CCTMXObjectGroup *group = [_tileMap objectGroupNamed:@"Objects"];
        NSMutableDictionary *P = [group objectNamed:@"P"];
        int x = [[P valueForKey:@"x"] intValue];
        int y = [[P valueForKey:@"y"] intValue];
    
        _player = [[GameMovingObject objectWithImage:@"C.png" row:4 column:4 size:CGSizeMake(32, 48)] retain];
        [_player sprite].position=ccp(x, y);
        [self addChild:[_player sprite]];
        
        [[SimpleAudioEngine sharedEngine] preloadEffect:@"move.caf"];
        [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"TileMap.caf"];
        
        [self schedule:@selector(lifeSchedule) interval:1];
        
        self.isTouchEnabled = YES;

	}
	return self;
}

-(void)registerWithTouchDispatcher{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(void)lifeSchedule{
    _player.life = _player.life - 1;
    [self.glayer lifeValueChange:_player.life];
    if (_player.life <= 0.0f) {
        [self unschedule:@selector(lifeSchedule)];
        [self.glayer showGameOver];
        [[CCDirector sharedDirector] pause];
    }
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    
    return YES;
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint to = [self convertTouchToNodeSpace:touch];
    [_player sendAction:MovingObject_Action_Move object:[NSValue valueWithCGPoint:to]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"move.caf"];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
    [_tileMap release];
    [_backGroundLayer release];
    [_player release];
	
	// don't forget to call "super dealloc"
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
	[super dealloc];
}

#pragma mark GameKit delegate

-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}
@end
